<?php

namespace Biz\Resources\Site;

use Lumen\Http\Resources\Json\Resource;
use Biz\Constants\Site\SiteMenuConstants;

/**
 * SiteMenuResource.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class SiteMenuResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return [
            'uid'            => $this->uid,
            'url'            => $this->url,
            'module'         => $this->module,
            'position'       => $this->position,
            'target_id'      => $this->target_id,
            'status'         => $this->status,
            'status_name'    => SiteMenuConstants::$StatusNames[$this->status],
            'created_at'     => $this->created_at->getTimestamp(),
            $this->mergeWhen($this->relationLoaded('parent'), [
                'parent' => $this->getParentSiteMenuResource(),
            ]),
            $this->mergeWhen($this->relationLoaded('children'), [
                'children' => $this->getChildrenSiteMenuResourceCollection(),
            ]),
            $this->mergeWhen($this->relationLoaded('translations'), [
                'translations' => $this->getSiteMenuTranslationsResourceCollection(),
            ]),
        ];
    }

    /**
     * Get app resource.
     *
     * @return SiteMenuTranslationsResourceCollection|null
     */
    public function getParentSiteMenuResource()
    {
        if ($this->relationLoaded('parent')) {
            if (is_null($this->parent)) {
                return SiteMenuConstants::PARENT_ROOT;
            } else {
                return new SiteMenuResource($this->parent);
            }
        }

        return null;
    }

    /**
     * Get app resource.
     *
     * @return SiteMenuResourceCollection|null
     */
    public function getChildrenSiteMenuResourceCollection()
    {
        if ($this->relationLoaded('children')) {
            return new SiteMenuResourceCollection($this->children);
        }

        return null;
    }

    /**
     * Get app resource.
     *
     * @return SiteMenuTranslationsResourceCollection|null
     */
    public function getSiteMenuTranslationsResourceCollection()
    {
        if ($this->relationLoaded('translations')) {
            return new SiteMenuTranslationsResourceCollection($this->translations);
        }

        return null;
    }
}
